package com.bvgol.examples.my.lambda;/**
 * @Classname Reduce
 * @Description TODO
 * @Date 2021/11/23 10:45
 * @Created by GUOCHEN
 */

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @program: my-spring-completely
 * @description:
 * @author: GUOCHEN
 * @create: 2021/11/23 10:45
 */
public class ReduceTest {
    public static void main(String[] args) {
//        ReduceTest reduceTest = new ReduceTest();
//        reduceTest.getCountType();
        ReduceTest.arraysSum();

    }

    public static void arraysSum(){
       int[]  args = new int[]{1, 2, 3, 4};
        int asInt = Arrays.stream(args).reduce(Integer::sum).getAsInt();
        System.out.println("asInt = " + asInt);
    }


    List<CartItemVo> items = Arrays.asList(CartItemVo.builder().skuId(1L).count(1).build(),
            CartItemVo.builder().skuId(2L).count(2).build(),
            CartItemVo.builder().skuId(3L).count(3).build(),
            CartItemVo.builder().skuId(4L).count(4).build());

    public Integer getCountType() {
        int count = 0;
        int count2 = 0;
        if (items != null && items.size() > 0) {
            for (CartItemVo item : items) {
                count += 1;
            }
            Optional<Integer> reduce = items.stream().map(CartItemVo::getCount).reduce((integer, integer2) -> {
                System.out.println(integer+"------"+integer2);
                return 1;
            });
            for (CartItemVo item : items) {
                count2 += item.getCount();
            }
            System.out.println(count2);
                Optional<Integer> reduce2 = items.stream().map(CartItemVo::getCount).reduce(Integer::sum);
                System.out.println(reduce2.get());

        }
        return count;
    }






}

@AllArgsConstructor
@NoArgsConstructor
@Builder
class CartItemVo {
    public Long getSkuId() {
        return skuId;
    }

    public void setSkuId(Long skuId) {
        this.skuId = skuId;
    }

    public Boolean getCheck() {
        return check;
    }

    public void setCheck(Boolean check) {
        this.check = check;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public List<String> getSkuAttrValues() {
        return skuAttrValues;
    }

    public void setSkuAttrValues(List<String> skuAttrValues) {
        this.skuAttrValues = skuAttrValues;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public void setTotalPrice(BigDecimal totalPrice) {
        this.totalPrice = totalPrice;
    }

    private Long skuId;

    private Boolean check = true;

    private String title;

    private String image;

    /**
     * 商品套餐属性
     */
    private List<String> skuAttrValues;

    private BigDecimal price;

    private Integer count;

    private BigDecimal totalPrice;
    /**
     * 计算当前购物项总价
     * @return
     */
    public BigDecimal getTotalPrice() {

        return this.price.multiply(new BigDecimal("" + this.count));
    }

}

